#include "state_rpgtransition.h"

void CStateRPGTransition::Init()
{

	m_Tag = "StateRPGTransition";
	
	m_bStarted = true;

}


void CStateRPGTransition::PreRun()
{

	m_fBlackWidth = 0.0f;
	m_fWhiteAlpha = 0.0f;
	
	m_bDrawBattle = false;

	m_StartTime = SDL_GetTicks();
	
	m_iStateMessage = 1;
	
	m_pGameInterface->BattleSystem()->BindWithEntityFactory();
	
}


void CStateRPGTransition::Input()
{

	SDL_Event ev;
	
	SDL_PollEvent( &ev );
	
	Uint8 * keys = SDL_GetKeyState( NULL );
	
	if( ev.key.keysym.sym == SDLK_ESCAPE )
	{

		m_iStateMessage = 0;
		
	}
	
	if( ev.type == SDL_QUIT )
	{
	
		m_iStateMessage = 0;
		
	}
	
	m_pGameInterface->BattleSystem()->Input( ev, keys );
	
}

void CStateRPGTransition::Think()
{
	
	
	if( m_fBlackWidth < SCREEN_WIDTH ) 
		m_fBlackWidth += SCREEN_WIDTH * 4 * m_pGameInterface->GetDPS();
	
	if( !m_bDrawBattle )
	{
		
		if( SDL_GetTicks() - m_StartTime > 400 )
		{
		
			if( m_fWhiteAlpha < 1.0f )
				m_fWhiteAlpha += 4.0f * m_pGameInterface->GetDPS();
			else 
				m_bDrawBattle = true;
		
		}
	
	} else
	{
		
		if( m_fWhiteAlpha > 0.0f )
			m_fWhiteAlpha -= 2.0f * m_pGameInterface->GetDPS();
		
	}
	
	m_pGameInterface->BattleSystem()->Think();

}

void CStateRPGTransition::Draw()
{

	CLEAR_BUFFER;

	PUSH_MATRIX;
	
	m_pGameInterface->GetGameCamera()->Translate( m_pGameInterface->GetDPS() );
	
	m_pGameInterface->GetMap()->DrawFrameBufferObjects( m_pGameInterface->GetGameCamera()->GetCurTranslation() );

	POP_MATRIX;
	
	m_pGameInterface->GetFrameScene()->Draw();
	
	if( m_bDrawBattle )
	{
		
		m_pGameInterface->BattleSystem()->DrawBackground();
		m_pGameInterface->BattleSystem()->DrawEntities();
		
		if( SDL_GetTicks() - m_StartTime > 2000 )
		{
			
			m_iStateMessage = STM_GOTO_RPGBATTLE;
			
		}
		
	}
	
	m_pGameInterface->CallEngineHook( "PreBlackBarRPGBattleDraw" );

	Draw::DisableTextures();
	
	Draw::Color4f( 1.0f, 1.0f, 1.0f, m_fWhiteAlpha );
	Draw::Quad( 0.0f, BATTLETRANSITION_HEIGHT, 0.0f, SCREEN_WIDTH, SCREEN_HEIGHT - BATTLETRANSITION_HEIGHT * 2.0f );
				
	Draw::Color4f( 0.0f, 0.0f, 0.0f, 1.0f );
	Draw::Quad( 0.0f, 0.0f, 0.0f, m_fBlackWidth, BATTLETRANSITION_HEIGHT );
	Draw::Quad( SCREEN_WIDTH - m_fBlackWidth, SCREEN_HEIGHT - BATTLETRANSITION_HEIGHT, 0.0f, m_fBlackWidth, BATTLETRANSITION_HEIGHT );
	
	Draw::EnableTextures();
	
	Draw::Color4f( 1.0f, 1.0f, 1.0f, 1.0f );
	
	SWAP_BUFFERS;
	
}